﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Windows;
using System.Threading;

using System.Diagnostics;
using System.Windows.Interop;
using System.Runtime.InteropServices;
namespace dmm
{
    /// <summary>
    /// Interaction logic for App.xaml
    /// </summary>
    public partial class App : Application
    {

    }


    //custom app starter
    class Startup
    {
        static Mutex mutex = new Mutex(false, "sothink.com Canvas2Video");

        
        [DllImport("user32.dll")]
        private static extern Boolean ShowWindow(IntPtr hWnd, Int32 nCmdShow);

        [DllImport("user32.dll")]
        private static extern bool SetForegroundWindow(IntPtr hWnd);

        [STAThread]
        static void Main()
        {
            Process currentProcess = Process.GetCurrentProcess();
            var runningProcess = (from process in Process.GetProcesses()
                                  where
                                    process.Id != currentProcess.Id &&
                                    process.ProcessName.Equals(
                                      currentProcess.ProcessName,
                                      StringComparison.Ordinal)
                                  select process).FirstOrDefault();
            if (runningProcess != null)
            {
                ShowWindow(runningProcess.MainWindowHandle, 1);
                SetForegroundWindow(runningProcess.MainWindowHandle);
                return;
            }
            else
            {
                App app = new App();
                app.MainWindow = new Window1();
                app.MainWindow.Show();
                app.Run();
            }
        }

       /* static void Main()
        {
            if (!mutex.WaitOne(TimeSpan.FromSeconds(5), false))
            {
                Console.WriteLine("Another instance of the app is running. Bye!");
                return;
            }

            try
            {
                App app = new App();
                app.MainWindow = new Window1();
                app.MainWindow.Show();
                app.Run();
            }
            finally
            {
                mutex.ReleaseMutex();
            }    
        }*/
    }
}
